define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
    "Date", "DateCN", "css!DateCss",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, SelectOrg, SearchBlock) {

    var sysPath = getServer() + "/static/app/fxpg/fxVulnerabilityDanger";

    var fxVulnerabilityDangerFormHtml = sysPath + "/views/fxVulnerabilityDangerForm.html";
    var getFxVulnerabilityDangerController = function () {
        return getServer() + "/fxVulnerabilityDangers";
    };

    /**
     * 页面初始化
     */
    var init = function () {
        initSearchBlock();
        queryBtnBind();
        initProvinceSelect("北京市", "北京市", "");
    };

    /**
     * 搜索栏初始化省市区
     */
    var initProvinceSelect = function (Province, City, Area) {
        //debugger;
        var html = "";
        $("#citySelect").append(html);
        $("#areaSelect").append(html);
        $.ajax({
            url: getServer() + "/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    if (item.divisionName == Province) {
                        //debugger;
                        var divisionId = item.divisionId;
                        var htmlCity = '';
                        if (Province == "北京市" || Province == "天津市" || Province == "上海市" || Province == "重庆市") {
                            $("#citySelect").append("<option value='" + Province + "' exid='" + divisionId + "'>" + Province + "</option>");
                            $('#citySelect').val(City);
                            var htmlArea = '';
                            $.ajax({
                                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                                contentType: "application/json",
                                type: "get",
                                success: function (data) {
                                    $.each(data, function (idx, item) {
                                        htmlArea += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                    });
                                    $("#areaSelect").append(htmlArea);
                                    $('#areaSelect').val(Area);
                                    // 列表
                                    createFxVulnerabilityDangerGrid();
                                }
                            });

                        } else {
                            $.ajax({
                                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                                contentType: "application/json",
                                type: "get",
                                success: function (data) {
                                    //debugger;
                                    $.each(data, function (idx, item) {
                                        if (item.divisionName == City) {
                                            var divisionId = item.divisionId;
                                            var htmlArea = '';
                                            $.ajax({
                                                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                                                contentType: "application/json",
                                                type: "get",
                                                success: function (data) {
                                                    $.each(data, function (idx, item) {
                                                        htmlArea += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                                    });
                                                    $("#areaSelect").append(htmlArea);
                                                    $('#areaSelect').val(Area);
                                                }
                                            });
                                        }
                                        htmlCity += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                    });
                                    $("#citySelect").append(htmlCity);
                                    $('#citySelect').val(City);
                                }
                            });
                        }
                    }
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#provinceSelect").append(html);
                $('#provinceSelect').val(Province);
            }
        });

        $("#provinceSelect").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#citySelect option").remove();
            $("#citySelect").append(html);
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#citySelect").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#citySelect").append(html);
                }
            });
        });
        $("#citySelect").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer() + "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#areaSelect").append(html);
                }
            });
        });
    };

    var initSearchBlock = function () {
        SearchBlock.init("searchBlock");
    };
    var queryBtnBind = function () {
        $("#btnSearch").click(function () {
            createFxVulnerabilityDangerGrid();
        });
        $("#btnReset").click(function () {
//          $("#provinceSelect").val("");
//          $("#citySelect").val("");
//          $("#areaSelect").val("");
			var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#citySelect option").remove();
            $("#citySelect").append(html);
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
			initProvinceSelect("北京市", "北京市", "");
        });
    };

    var createFxVulnerabilityDangerGrid = function () {
        $("#fxVulnerabilityDangerGrid").datagrid({
            url: getFxVulnerabilityDangerController() + "/queryFxVulnerabilityDangers",
            method: "GET",
            fitColumns: true,
            autoRowHeight: false,
            columns: [[
                {
                    field: 'province',
                    title: '省',
                    width: '14%',
                    align: 'center',
                },
                {
                    field: 'city',
                    title: '市',
                    width: '15%',
                    align: 'center',
                },
                {
                    field: 'area',
                    title: '区',
                    width: '15%',
                    align: 'center',
                    formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxVulnerabilityDanger(\'' + rowData.id + '\');"> ' + rowData.area + ' </a> '
                    }
                },
                {
                    field: 'viEarthquakeIntensity',
                    title: '地震动强度VI发生概率',
                    width: '11%',
                    align: 'center',
                    /*formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxVulnerabilityDanger(\'' + rowData.id + '\');"> ' + rowData.viEarthquakeIntensity + ' </a> '
                    }*/
                },
                {
                    field: 'viiEarthquakeIntensity',
                    title: '地震动强度VII发生概率',
                    width: '11%',
                    align: 'center',
                    /*formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxVulnerabilityDanger(\'' + rowData.id + '\');"> ' + rowData.viiEarthquakeIntensity + ' </a> '
                    }*/
                },
                {
                    field: 'viiiEarthquakeIntensity',
                    title: '地震动强度VIII发生概率',
                    width: '12%',
                    align: 'center',
                    /*formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxVulnerabilityDanger(\'' + rowData.id + '\');"> ' + rowData.viiiEarthquakeIntensity + ' </a> '
                    }*/
                },
                {
                    field: 'ixEarthquakeIntensity',
                    title: '地震动强度IX发生概率',
                    width: '11%',
                    align: 'center',
                    /*formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxVulnerabilityDanger(\'' + rowData.id + '\');"> ' + rowData.ixEarthquakeIntensity + ' </a> '
                    }*/
                },
                {
                    field: 'xearthquakeIntensity',
                    title: '地震动强度X发生概率',
                    width: '11%',
                    align: 'center',
                    /*formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxVulnerabilityDanger(\'' + rowData.id + '\');"> ' + rowData.xearthquakeIntensity + ' </a> '
                    }*/
                },
            ]],
            toolbar: [/*{
                iconCls: 'fa fa-plus-circle',
                text:"添加",
                handler: function(){
                    addFxVulnerabilityDanger();
                }
            },*/ {
                iconCls: 'fa fa-plus-circle',
                text: "模板",
                handler: function () {
                    //downloadTemplate();
                    Util.alert("此功能暂未实现");
                }
            }, {
                iconCls: 'fa fa-upload',
                text: "导入",
                handler: function () {
                    importForm();
                    //Util.alert("此功能暂未实现");
                }
            }, {
                iconCls: 'fa fa-download',
                text: "导出",
                handler: function () {
                    exportForm();
                }
            }/*, {
                iconCls: 'fa fa-trash-o',
                text:"删除",
                handler: function(){
                    deleteFxVulnerabilityDanger();
                }
            }*/],
            queryParams: {
                province: $("#provinceSelect").val(),
                city: $("#citySelect").val(),
                area: $("#areaSelect").val(),
            },
            pagination: true,
            pageSize: 10
        });
    };

    /**
     * 导出
     */
    var exportForm = function () {
        let province = $("#provinceSelect").val();
        let city = $("#citySelect").val();
        let area = $("#areaSelect").val();
        window.location.href = getFxVulnerabilityDangerController() + "/export?province=" + province + "&city="
            + city + "&area=" + area;
    };

    /**
     * 导入
     */
    var importForm = function () {

		$("#uploadModal").modal();
		$("#uploadModal").on("shown.bs.modal", function () {
			var url = getServer() + "/excel/航、卫片解译线-线.xls";
			$("#downloadZwExcelTemplate").attr("href", url);
		});

		$('#uploadButton').off("click");
		$("#uploadButton").on("click", function () {
			var uploadinput = document.getElementById("uploadFile");
			if (uploadinput.value == "") {
				Util.alert("上传前请先选择文件!");
				return;
			}
			var formData = new FormData();
			formData.append("file", uploadinput.files[0]);
			$.ajax({
				url: getFxVulnerabilityDangerController() + "/import",
				data: formData,
				processData: false, //因为data值是FormData对象，不需要对数据做处理。
				contentType: false,
				type: "POST",
				success: function (data) {
					$('#uploadModal').modal('hide');
					uploadinput.value = null;
					Util.alert(data);
					createFxVulnerabilityDangerGrid();
				}
			});
		});
	};

    var formValidator = function () {
        $("#fxVulnerabilityDangerForm").validate({
            rules: {
                province: {
                    required: true,
                },
                city: {
                    required: true,
                },
                area: {
                    required: true,
                },
                viEarthquakeIntensity: {
                    required: true,
                },
                viiEarthquakeIntensity: {
                    required: true,
                },
                viiiEarthquakeIntensity: {
                    required: true,
                },
                ixEarthquakeIntensity: {
                    required: true,
                },
                xEarthquakeIntensity: {
                    required: true,
                },
            },
            messages: {
                province: {
                    required: "省不允许为空!",
                },
                city: {
                    required: "市不允许为空!",
                },
                area: {
                    required: "区不允许为空!",
                },
                viEarthquakeIntensity: {
                    required: "地震动强度VI发生概率不允许为空!",
                },
                viiEarthquakeIntensity: {
                    required: "地震动强度VII发生概率不允许为空!",
                },
                viiiEarthquakeIntensity: {
                    required: "地震动强度VIII发生概率不允许为空!",
                },
                ixEarthquakeIntensity: {
                    required: "地震动强度IX发生概率不允许为空!",
                },
                xEarthquakeIntensity: {
                    required: "地震动强度X发生概率不允许为空!",
                },
            }
        });
    };
    var getFxVulnerabilityDanger = function (id) {
        $.ajax({
            url: getFxVulnerabilityDangerController() + "/" + id,
            type: "get",
            success: function (data) {
                editProvince(data.province, data.city, data.area);
                Tool.deserialize("fxVulnerabilityDangerForm", data);
            }
        });
    };

    // 编辑时加载省市区
    var editProvince = function (Province, City, Area) {
        debugger;
        var html = "";
        $("#city").append(html);
        $("#area").append(html);
        $.ajax({
            url: getServer() + "/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    if (item.divisionName == Province) {
                        debugger;
                        var divisionId = item.divisionId;
                        var htmlCity = '';
                        if (Province == "北京市" || Province == "天津市" || Province == "上海市" || Province == "重庆市") {
                            $("#city").append("<option value='" + Province + "' exid='" + divisionId + "'>" + Province + "</option>");
                            $('#city').val(City);
                            var htmlArea = '';
                            $.ajax({
                                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                                contentType: "application/json",
                                type: "get",
                                success: function (data) {
                                    $.each(data, function (idx, item) {
                                        htmlArea += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                    });
                                    $("#area").append(htmlArea);
                                    $('#area').val(Area);
                                }
                            });

                        } else {
                            $.ajax({
                                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                                contentType: "application/json",
                                type: "get",
                                success: function (data) {
                                    debugger;
                                    $.each(data, function (idx, item) {
                                        if (item.divisionName == City) {
                                            var divisionId = item.divisionId;
                                            var htmlArea = '';
                                            $.ajax({
                                                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                                                contentType: "application/json",
                                                type: "get",
                                                success: function (data) {
                                                    $.each(data, function (idx, item) {
                                                        htmlArea += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                                    });
                                                    $("#area").append(htmlArea);
                                                    $('#area').val(Area);
                                                }
                                            });
                                        }
                                        htmlCity += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                    });
                                    $("#city").append(htmlCity);
                                    $('#city').val(City);
                                }
                            });
                        }
                    }
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#province").append(html);
                $('#province').val(Province);
            }
        });
        $("#province").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#city option").remove();
            $("#city").append(html);
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#city").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#city").append(html);
                }
            });
        });
        $("#city").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#area").append(html);
                }
            });
        });
    }

    var addFxVulnerabilityDanger = function () {
        var slidebar = Util.slidebar({
            url: fxVulnerabilityDangerFormHtml,
            width: "580px",
            cache: false,
            close: true,
            afterLoad: function () {
                formValidator();
                $("#saveBtn").on("click", function () {
                    if ($("#fxVulnerabilityDangerForm").valid()) {
                        var data = Tool.serialize("fxVulnerabilityDangerForm");
                        $.ajax({
                            url: getFxVulnerabilityDangerController(),
                            contentType: "application/json",
                            data: JSON.stringify(data),
                            type: "post",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                createFxVulnerabilityDangerGrid();
                            }
                        });
                    }
                });
            }
        });
    };

    window._editFxVulnerabilityDanger = function (fxVulnerabilityDangerId) {
        var slidebar = Util.slidebar({
            url: fxVulnerabilityDangerFormHtml,
            width: "580px",
            cache: false,
            close: true,
            afterLoad: function () {
                formValidator();
                getFxVulnerabilityDanger(fxVulnerabilityDangerId);
                $("#saveBtn").on("click", function () {
                    if ($("#fxVulnerabilityDangerForm").valid()) {
                        var data = Tool.serialize("fxVulnerabilityDangerForm");
                        $.ajax({
                            url: getFxVulnerabilityDangerController(),
                            contentType: "application/json",
                            data: JSON.stringify(data),
                            type: "put",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                createFxVulnerabilityDangerGrid();
                            }
                        });
                    }
                });
            }
        });
    };
    var deleteFxVulnerabilityDanger = function () {
        var rows = $("#fxVulnerabilityDangerGrid").datagrid("getSelections");
        if (rows == null || rows.length == 0) {
            Util.alert("请选择一行数据!");
            return;
        }
        Util.confirm("是否要删除选中的数据?", function () {
            var ids = "";
            $.each(rows, function (i, row) {
                ids += row.id + ",";

            });
            ids = ids.substr(0, ids.length - 1);
            $.ajax({
                url: getFxVulnerabilityDangerController(),
                data: {
                    ids: ids
                },
                type: "delete",
                success: function (data) {
                    createFxVulnerabilityDangerGrid();
                }
            });
        }, function () {
            return;
        });

    };

    return {
        init: init
    };
});
